﻿using Dapper;
using HR.Config;
using HR.IService;
using HR.Models;
using System.Data.SqlClient;

namespace HR.Service
{
    public class RolesService : IRolesService
    {
        private readonly HRDBContext db;
        private readonly IConfiguration conf;
        private readonly string? strcon;

        public RolesService(HRDBContext db, IConfiguration conf)
        {
            this.db = db;
            this.conf = conf;
            strcon = this.conf["ConnectionStrings"];
        }

        public Task<List<Role>> RoleFillRoles()
        {
            return Task.Run(() =>
            {
                List<Role> list = db.roles.ToList();
                return list;
            });
        }

        public Task<Paging<Role>> RoleFillRolesPG(int pgsize, int pgcurrent)
        {
            return Task.Run(() =>
            {
                Paging<Role> pg = new Paging<Role>();
                List<Role> list = db.roles.ToList();
                List<Role> tabs = list.Skip((pgcurrent - 1) * pgsize).Take(pgsize).ToList();
                pg.tabs = tabs;
                pg.rows = list.Count;
                return pg;
            });
        }
        public Task<int> RoleInsertRole(Role re)
        {
            return Task.Run(() =>
            {
                db.roles.Add(re);
                return db.SaveChanges();
            });
        }
        /// <summary>
        /// 根据角色ID查询到的拥有权限菜单,此处为List<int>集合，用于绑定已有的键值Key回显
        /// </summary>
        /// <param name="re"></param>
        /// <returns></returns>
        public Task<List<int>> FindMenusByRid(int rid)
        {
            return Task.Run(() =>
            {
                List<int> list = new List<int>();
                //此处查询的是该角色ID下的所有非父级菜单的所有子集ptype为1(true)
                using (SqlConnection sqlcon = new SqlConnection(strcon))
                {
                    string sql = $@"select m.mid from [dbo].[RoleMenuMid] rm inner join[dbo].[Menu] m
                    on rm.mid = m.Mid where ptype=1 and rid = {rid}";
                    list= sqlcon.Query<int>(sql).ToList();
                }
                return list;
            });
        }

        public Task<int> RoleUpdateRole(Role re)
        {
            return Task.Run(() =>
            {
                Role? rl = db.roles.Find(re.rid);
                rl.rname = re.rname;
                return db.SaveChanges();
            });
        }

        public Task<int> RoulMenuMidUdpOrAdd(List<RoleMenuMid> list)
        {
            return Task.Run(() =>
            {
                //先查询出来该角色原拥有的所有权限
                List<RoleMenuMid> rmms = db.rolemenumids.Where(rm => rm.Rid == list[0].Rid).ToList();
                //遍历集合删除原有的所有权限信息
                foreach (RoleMenuMid rmm in rmms)
                {
                    db.rolemenumids.Remove(rmm);
                }
                //再遍历新增的权限信息集合
                foreach (RoleMenuMid rm in list)
                {
                    db.rolemenumids.Add(rm);
                }
                return db.SaveChanges();
            });
        }

        public Task<int> RoleDeleteRole(int rid)
        {
            return Task.Run(() =>
            {
                List<user> users = db.users.Where(ur => ur.rid == rid).ToList();
                //这里先判断这个角色是否有被用户引用分配，有的话无法被删除
                if (users.Count > 0)
                {
                    return 500;
                }
                else
                {
                    //先将该角色下已经分配的权限进行删除
                    List<RoleMenuMid> list = db.rolemenumids.Where(rm => rm.Rid == rid).ToList();
                    foreach (RoleMenuMid item in list)
                    {
                        db.rolemenumids.Remove(item);
                    }
                    Role? re = db.roles.Find(rid);
                    db.roles.Remove(re);
                    return db.SaveChanges();
                }
            });
        }
    }
}
